package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.GreTreePatrolLogFileDao;
import cn.topevery.garden.entity.po.GreTreePatrolLogFile;
import cn.topevery.garden.entity.query.GreTreePatrolLogFileQuery;
import cn.topevery.garden.entity.vo.GreTreePatrolLogFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.entity.Value;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author code-generate
 * @date 2022-02-27
 */
@Repository
public class GreTreePatrolLogFileDaoImpl extends BaseDaoImpl<GreTreePatrolLogFile> implements GreTreePatrolLogFileDao {
    @Override
    public <P extends DbCsp> List<GreTreePatrolLogFile> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<GreTreePatrolLogFileVO> listPage(GreTreePatrolLogFileQuery query) {
        String table = "t_gre_tree_patrol_log_file tgtplf";

        String columns = "tgtplf.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tgtplf.c_id desc");
        sqlQueryMate
                .where(true, "tgtplf.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, GreTreePatrolLogFileVO.class);
    }

    @Override
    public GreTreePatrolLogFile getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<GreTreePatrolLogFile> getList(Integer id) {
//        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_gre_tree_patrol_log_file tgtplf").setColumns("tgtplf.*");
//        sqlQueryMate.where(true, "tgtplf.c_db_status", ScriptConditionEnum.Eq, 0)
//                .where(id != null, "tgtplf.c_main_id", ScriptConditionEnum.Eq, id);
        String sql = "SELECT *  from  t_gre_tree_patrol_log_file where c_db_status = 0 and c_main_id =" + id;
        Value<List<GreTreePatrolLogFile>> result = new Value<>();
        run(sql, CommandType.Text, dbCommand -> {
            result.data = dbCommand.executeToList(GreTreePatrolLogFile.class);
        });
        return result.data;
    }

    @Override
    public void deleteByPid(Integer id) {
        String sql = "delete from t_gre_tree_patrol_log_file where c_main_id =" + id;
        run(sql, CommandType.Text, dbCommand -> dbCommand.executeNonQuery());
    }
}
